package Model.QBE;

import Model.SQL.*;
import java.util.*;
import java.sql.*;
import Model.Beans.*;
import Model.Beans.Results.*;


public class CourseModel extends DataAccessHelperBase {
    
    public CourseModel() 
    {
        String tableName = "course";        
        super.customize(tableName);
    }
    
    public int insert(Course course)
    {
        String[] values = new String[7];
        values[0] = "null";
        values[1] = "'" + course.getClassRoom().getId() + "'";
        values[2] = "'" + course.getProfessor().getId() + "'";
        values[3] = "'"  + course.getCodeCourse() + "'";
        values[4] = "'" + course.getTime() + "'";
        values[5] = "'" + course.getDays() + "'";
        values[6] = "'" + course.getTitle() + "'";
        
        return this.insertRecord(values);
    }
    
    public int delete(String id)
    {
        return this.deleteRecord("course_id =" + id);
    }
    
    public int delete(Course course)
    {
        return this.delete(Integer.toString(course.getId()));
    }
    
    public int modify(String id, String class_room, String professor, String course_code, String time, String days, String title)
    {
        String id_str = "course_id = " + id;
        String[] values = new String[6];
        
        int i = 0;
        if(class_room.length() > 0)
            values[i++] = "_classroom = '" + class_room + "'";
        if(professor.length() > 0)
            values[i++] = "_professor = '" + professor + "'";
        if(course_code.length() > 0)
            values[i++] = "course_code = '" + course_code + "'";
        if(time.length() > 0)
            values[i++] = "time = '" + time + "'";
        if(days.length() > 0)
            values[i++] = "days = '" + days + "'";
        if(title.length() > 0)
            values[i++] = "title = '" + title + "'";
        return this.updateRecord(id_str, values);
    }
    
    public int modify(Course course)
    {
        return this.modify(Integer.toString(course.getId()), Integer.toString(course.getClassRoom().getId()), 
                Integer.toString(course.getProfessor().getId()), course.getCodeCourse(), course.getTime(), course.getDays(), course.getTitle());
    }
    
    public CourseList search(String id, String class_room, String professor, String course_code, String time, String days, String title)
    {
        String where = "";
        ResultSet rs = null;
        CourseList result = new CourseList();
        ProfessorModel pmodel = new ProfessorModel();
        ClassroomModel cmodel = new ClassroomModel();
        
        if(id.length() > 0 && !id.equals("0"))
            where += "course_id = " + id + " AND ";
        if(class_room.length() > 0 && !class_room.equals("0"))
            where += "_classroom = " + "'" + class_room + "' AND ";
        if(professor.length() > 0 && !professor.equals("0"))
            where += "_professor = " + "'" + professor + "' AND ";
        if(course_code.length() > 0)
            where += "course_code = " + "'" + course_code + "' AND ";
        if(time.length() > 0)
            where += "time = " + "'" + time + "' AND";
        if(days.length() > 0)
            where += "days = " + "'" + days + "' AND";
        if(title.length() > 0)
            where += "title = " + "'" + title + "'";
        else if(where.length() > 0)
            where = where.substring(0, where.length()-4);
        
        
        rs = this.getData(where);
        
        if(rs == null)
            return result;
        try 
        {
            while(rs.next()) 
            {
                int _course_id = rs.getInt("course_id");
                int _classroom = rs.getInt("_classroom");
                int _professor = rs.getInt("_professor");
                String _course_code = rs.getString("course_code");
                String _time = rs.getString("time");
                String _days = rs.getString("days");
                String _title = rs.getString("title");
                
                //Crear objeto temporal curso
                Course tmpCourse = new Course();
                
                tmpCourse.setClassRoom(cmodel.search_by_id(Integer.toString(_classroom)));
                tmpCourse.setCodeCourse(_course_code);
                tmpCourse.setDays(_days);
                tmpCourse.setId(_course_id);
                tmpCourse.setProfessor(pmodel.search_by_id(Integer.toString(_professor)));
                tmpCourse.setTime(_time);
                tmpCourse.setTitle(_title);
                
                //Anadirlo a la lista
                result.addCourse(tmpCourse);
                
                System.out.print(_course_id + " " + _classroom + " " + _professor + " ");
                System.out.println(_course_code + " " + _time + " " + _days + " " + _title);
            }
            return result;
        }
        catch(SQLException e)
        {
            System.out.println("SQLException: " + e.getMessage());
        }   
        finally
        {
            if (rs != null) 
            {
                try 
                {
                    rs.close();
                } catch (SQLException sqlEx) { }
                rs = null;
            }
        }
        return result;
    }
    
    public CourseList search(Course course)
    {
        return this.search(Integer.toString(course.getId()), Integer.toString(course.getClassRoom().getId()), 
                Integer.toString(course.getProfessor().getId()), course.getCodeCourse(), course.getTime(), course.getDays(), course.getTitle());
    }
    
    public Course search_by_id(String id)
    {
        CourseList list = new CourseList();
        Course course = new Course();
        course.setId(Integer.parseInt(id));
        
        list = this.search(course);
        
        if(list.getCourses().size() > 0)
            return (Course)(list.getCourses().get(0));
        else
            return null;
    }
    
    /*
    public static void main(String[] args)
    {
        CourseModel course = new CourseModel();
        Course c = new Course();
        c.setClassRoom(new ClassRoom(10, "02", 114));
        c.setCodeCourse("IS-EA");
        c.setDays("LMV");
        c.setProfessor(new Person(999999, "Gerardo", "Ayala", "San Martin"));
        c.setTime("10:00-12:00");
        c.setId(5);
        c.setTitle("Nueva materia");
        //c.setId();
        //course.insert(c);
        course.modify(c);
        //course.search(c);
        
        //c = course.search_by_id("5");
        //System.out.println(c.toString());
        
        
        //CourseList list = course.search(new Course());
        
        //System.out.println("LISTA");
        //for(int i=0; i<list.getCourses().size(); i++)
        {
            //Course tmp = (Course)(list.getCourses().get(i));
            //System.out.println(tmp.toString() + " " + tmp.getClassRoom().toString() +" " + tmp.getProfessor().toString());
        } 
    }
     */
}
